clear
clc
%=================PRIPRAVA DAT
%cd /home/jura/trunk/agentOctave;
%mData = load("clarinet_talking_3d_zcr_octave.csv"); %spatne
%mData = load("clarinet_talking_3d_freq_octave.csv"); %???
mData = load("heavy_silence_3d_std_octave_bin.csv"); %dobre

%nastaveni poctu vystupnich neuronu
output_neuron_count=1;

%viz http://stackoverflow.com/questions/2976452/whats-the-diference-between-train-validation-and-test-set-in-neural-networks
%TRENOVACI (pri uceni upravuji vahy)
%(data, pocet_vystupnich_hodnot, optimalizace (nahodné pořadí + min a max), pomer testovacich dat, pomer validacnich dat)

[mTrain, mTest, mVali] = subset(mData', output_neuron_count, 1, 1/3, 1/6); 

%mTrain = mData';
%mTest = mData';
%mVali = mData';

%==================STANDARDIZACE
%standardizace trenovaciho vstupu
%[mTrainInputStandard, cMeanInput, cStdInput] = prestd(mTrain(1:end-1, :));
%mTrainOutputStandard = mTrain(end, :);
%mMinMaxElements = min_max(mTrainInputStandard);

%VALIDACNI (pri uceni se pouzivaji na zkousku, jestli neni sit preucena -- umi generalizovat)
%VV.P = mVali(1:end-1, :);
%VV.T = mVali(end, :);
%standardizace validacniho vstupu
%VV.P = trastd(VV.P, cMeanInput, cStdInput);

%TESTOVACI (pri testovani na overeni nauceni site)
%mTestInputStandard = trastd(mTest(1:end-1, :), cMeanInput, cStdInput);

%===================BEZ STANDARDIZACE
Train.P=mTrain(1:end-output_neuron_count, :); 	%train input
Train.T=mTrain(end, :);			%train target

Test.P=mTest(1:end-output_neuron_count, :);	%test input
Test.T=mTest(end, :);			%test target

VV.P = mVali(1:end-output_neuron_count, :);	%validation input
VV.T = mVali(end, :);			%validation target

mMinMaxElements = min_max(Train.P);

%=================PRIPRAVA MLP
%pocety neuronu ve skrytych vrstvach a ve vystupni
topology = [14 7 output_neuron_count];

MLPNet = newff(mMinMaxElements, topology, {"tansig", "purelin", "tansig"}, "trainlm", "not used", "mse");
%saveMLPStruct(MLPNet, "MLPNet.txt");

%=================TRENOVANI MLP
net = train(MLPNet, Train.P, Train.T, [], [], VV);
%saveMLPStruct(net, "MLPNet.txt");

%=================TESTOVANI MLP
simOut = sim(net, Test.P);
simOutDis = round(simOut);

for i=1:length(mTest(1,:))
  if (mTest(end,i)==simOutDis(i))
    shoda(i)=1;
  else
    shoda(i)=0;
  endif	
end

uspesnost=sum(shoda)/length(shoda)*100

%=================POROVNANI LABELU A VYSLEDKU SITE
csvwrite('MLPvysledek.csv', [mTest' simOut' shoda']);
